package com.sun;

import java.util.Objects;

// Equals学习
public class EqualsTest1 {

    public static void main(String[] args) {
        // 新建两个内容相同的Person对象
        Person person1 = new Person(12, "zhangSan");
        Person person2 = new Person(12, "zhangSan");

        // Person类里面重写了Object类里面的equals()方法，比较的是Person对象的属性是否相同
        System.out.println("person1.equals(person2 :"+person1.equals(person2));

        System.out.println("person1 == person2 :"+(person1 == person2));
    }

    private static class Person{
        int age;
        String name;

        public Person(int age, String name) {
            this.age = age;
            this.name = name;
        }

        @Override
        public boolean equals(Object o) {
            // 如果是同一个对象，则返回true
            if (this == o) return true;
            if (o == null || this.getClass() != o.getClass()) return false;
            Person person = (Person) o;
            return age == person.age && Objects.equals(name, person.name);
        }

        //@Override
        //public int hashCode() {
        //    return Objects.hash(age, name);
        //}

        @Override
        public String toString() {
            return "Person{" +
                    "age=" + age +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
}
